package com.ljf.eslab.lab.service.impl;

import com.ljf.eslab.lab.dto.CourseAndTeacherDTO;
import com.ljf.eslab.lab.entity.CourseEntity;
import com.ljf.eslab.lab.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljf.common.utils.PageUtils;
import com.ljf.common.utils.Query;

import com.ljf.eslab.lab.dao.StuCourseDao;
import com.ljf.eslab.lab.entity.StuCourseEntity;
import com.ljf.eslab.lab.service.StuCourseService;

@Slf4j
@Service("stuCourseService")
public class StuCourseServiceImpl extends ServiceImpl<StuCourseDao, StuCourseEntity> implements StuCourseService {
    @Autowired
    CourseService courseService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StuCourseEntity> page = this.page(
                new Query<StuCourseEntity>().getPage(params),
                new QueryWrapper<StuCourseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void insertByCids(Integer sid, List<Integer> cids) {
        List<CourseEntity> courses = courseService.getBaseMapper().selectList(new QueryWrapper<CourseEntity>().in("id", cids));
        for (CourseEntity course : courses) {
            StuCourseEntity stuCourseEntity = new StuCourseEntity();
            stuCourseEntity.setStuId(sid);
            stuCourseEntity.setCourseId(course.getId());
            stuCourseEntity.setCourseName(course.getCourseName());
            stuCourseEntity.setStuCount(course.getStuCount());
            stuCourseEntity.setMaxStu(course.getMaxStu());
            log.debug("插入选课记录:{}",stuCourseEntity);
            this.baseMapper.insertStuEntity(stuCourseEntity);
        }
    }

    @Override
    public PageUtils queryPageBySid(IPage<CourseAndTeacherDTO> params, Integer sid) {
        IPage<CourseAndTeacherDTO> page = courseService.pageMyCourses(params ,sid);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryCoursesPageBySid(IPage<CourseAndTeacherDTO> params, Integer sid) {
        //1.查询人数未满的课程
        IPage<CourseAndTeacherDTO> page = courseService.pageSelectableCourses(params);
        //2.查询学生已选的课程
        Set<Integer> choosenCids = this.baseMapper.selectList(new QueryWrapper<StuCourseEntity>().eq("stu_id", sid)).stream().map(StuCourseEntity::getCourseId).collect(Collectors.toSet());
        //3 过滤，在已选中的课程滤去
        List<CourseAndTeacherDTO> resList = page.getRecords().stream().filter(courseEntity -> {
            return !choosenCids.contains(courseEntity.getId());
        }).collect(Collectors.toList());
        page.setRecords(resList);
        return new PageUtils(page);
    }

    @Override
    public List<StuCourseEntity> selectByIds(List<Integer> ids) {
        return this.baseMapper.selectList(new QueryWrapper<StuCourseEntity>().in("id",ids));
    }

}